<?php

namespace app\admin\controller;

use app\common\model\GrowthLog;
use app\common\model\User as ModelUser;
use plug\SUtil;

class User extends Base
{
	public function __construct()
	{
		parent::__construct();
		$this->isLogin();
	}

	/**
	 * @route('user/userlist')
	 * 用户列表
	 */
	public function userlist()
	{
		// 获取搜索条件
		$get['utype'] = input('get.utype');
		$get['is_sales'] = 1;

		$ModelUser = new ModelUser();
		$field = "user_id,utype,nick_name,loginip,is_sales,avatar,lastlogin";

		$rs_data = $ModelUser->get_list_page($get,'user_id desc',$field,$this->limit);

		$tvar = array(
			"title" => "用户列表",
			"time"  => time(),
			"data"  => $rs_data,
			"get"   => $get,
		);
		$page = $rs_data->render();
		$this->assign("data", $tvar);
		$this->assign('page', $page);
		return $this->fetch('user/userlist');
	}

	/**
	 * @route('user/UpgradeSales')
	 * 用戶升级成业务员
	 */
	public function UpgradeSales(){
		$user_id = input('post.id');
		$data = [
			'is_sales' => 2,
			'sales_code' => SUtil::random(32),
			'sales_time' => time(),
		];
		$where = [
			'user_id' => $user_id
		];

		$ModelUser = new ModelUser();
		$rs = $ModelUser->save($data,$where);
		if($rs){
			$this->success('操作成功');
		}
		$this->error('操作失败');
	}

	/**
	 * @route('user/saleslist')
	 * 业务员列表
	 */
	public function saleslist()
	{
		// 获取搜索条件
		$ModelUser = new ModelUser();

        $field = "user_id,utype,nick_name,loginip,is_sales,avatar,lastlogin";

        $cond['is_sales'] = 2;
        $rs_data = $ModelUser->get_list_page($cond, 'user_id desc',$field,$this->limit);

		$tvar = array(
			"title" => "业务员列表",
			"time"  => time(),
			"data"  => $rs_data,
		);
		$page = $rs_data->render();
		$this->assign("data", $tvar);
		$this->assign('page', $page);
		return $this->fetch('user/saleslist');
	}

	/**
	 * @route('user/growthlog')
	 * 用户积分记录
	 */
	public function growthlog()
	{
	    $growthModel = new GrowthLog();
	    $userModel = new ModelUser();
        $cond['user_id'] = input('get.user_id');
        $userinfo = $userModel->getOne(['user_id'=>$cond['user_id']]);
        $rs_data = $growthModel->get_growth_record($cond,'*','log_id desc',$this->limit);

		$tvar = array(
			"title" => "积分记录列表",
			"time"  => time(),
			"data"  => $rs_data,
            "userinfo" => $userinfo,
            "types" => $growthModel::$typelist,
		);
		$page = $rs_data->render();
		$this->assign("data", $tvar);
		$this->assign('page', $page);
		return $this->fetch('user/growthlog');
	}
}